// pages/user/change-pwd/old-password.js
import { PHONE, CHECKMSG } from '../../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '', // 用户账号
    checkmsg: '',// 短信验证码
    ableSend: false, // 可否发送验证码
    msgvisi: false, // 正在发送验证码显示
    time: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '验证账号'
    })
  
  },
  // 输入账号
  inputPhone: function (e) {
    let value = e.detail.value
    let that = this
    if (PHONE.test(value)) {
      that.setData({
        ableSend: true,
        account: value
      })
    } else {
      wx.showToast({
        title: '账号不正确',
        icon: 'none'
      })
    }
  },
  // 输入验证码
  inputMsg: function(e) {
    let msg = e.detail.value
    let that = this;
    if (CHECKMSG.test(msg)) {
      that.setData({
        checkmsg: msg
      })
    } else {
      wx.showToast({
        title: '验证码错误',
        icon: 'none'
      })
    }
  },
  getCode: function (){
    var that = this;
    var currentTime = that.data.time;
    var interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime
      })
      if (currentTime <= 0) {
        that.setData({ msgvisi: false, time: 60}) 
        clearInterval(interval)
      }
    }, 1000)
  },
  // 发送验证码
  sendCode: function(){
    var that= this;
    if (that.data.ableSend) {
      that.setData({
        msgvisi: true
      })
      that.getCode()
    } else {
      wx.showToast({
        title: '输入正确的账号',
        icon: 'none'
      })
    }
  },
  // 下一步
  nextAction: function(){
    var that = this;
    if (that.data.account !== '' && that.data.checkmsg !== '')  {
      console.log('提交申请')
      wx.navigateTo({
        url: '../set-newpwd/index'
      })
    } else {
      wx.showToast({
        title: '请输入完整',
        icon: 'none'
      })
    }
  }

 
})